package com.fantazy.model;

import java.io.*;
import java.net.URL;

public class DownLoad {

//	private URL url;

	private File filePath = new File(ApplicationDatas.getSavePath());

	public DownLoad() {
	}



/*	public int downLoad() {
		InputStream instream = null;
		FileOutputStream fout = null;
		try {
			instream = url.openStream();
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		}

		try {
			fout = new FileOutputStream(
					new File(System.currentTimeMillis() + ""));
			byte buf[] = new byte[1024 * 2];

			int len = -1;

			while ((len = instream.read(buf)) != -1) {
				fout.write(buf, 0, len);
			}
			instream.close();
			fout.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return -1;
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		}
		return 1;
	}*/

	public int downLoad(String url) {
		InputStream instream = null;
		FileOutputStream fout = null;
		System.out.println(url);
		String fileType = url.substring(url.lastIndexOf("."), url.length());
		try {
			instream = new URL(url).openStream();
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		}

		try {
			fout = new FileOutputStream(
					new File(filePath.getAbsolutePath() + "\\" + System.currentTimeMillis() + fileType));
			System.out.println(filePath.getAbsolutePath() + "\\" + System.currentTimeMillis() + fileType);
			byte buf[] = new byte[1024 * 2];

			int len = -1;

			while ((len = instream.read(buf)) != -1) {
				fout.write(buf, 0, len);
			}
			instream.close();
			fout.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return -1;
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		}
		return 1;
	}

/*	public void downLoadAll() {
		for (String url : urls) {
			this.downLoad(url);
		}
	}*/
}

	
